package com.sixmac.controller.backend;

import com.sixmac.common.DataTableFactory;
import com.sixmac.controller.common.CommonController;
import com.sixmac.core.bean.Result;
import com.sixmac.entity.Opera;
import com.sixmac.service.OperaService;
import com.sixmac.utils.DateUtils;
import com.sixmac.utils.JsonUtil;
import com.sixmac.utils.WebUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/30 0030.
 */
@Controller
@RequestMapping(value = "backend/opera")
public class OperaController extends CommonController {

    @Autowired
    private OperaService operaService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
        return "backend/操作日志";
    }

    @RequestMapping("/list")
    public void list(HttpServletRequest request,
                     HttpServletResponse response,
                     String name,
                     String description,
                     String startTime,
                     String endTime,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);

        Date startDate = null;
        Date endDate = null;

        try {
            if (null == name) {
                name = "%" + name + "%";
            }

            if (null == description) {
                description = "%" + description + "%";
            }

            if (StringUtils.isNotEmpty(startTime)) {
                startDate = DateUtils.stringToDateWithFormat(startTime, "yyyy-MM-dd hh:mm:ss");
            }
            if (StringUtils.isNotEmpty(endTime)) {
                endDate = DateUtils.stringToDateWithFormat(endTime, "yyyy-MM-dd hh:mm:ss");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Page<Opera> page = operaService.page(name, description, startDate, endDate, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(HttpServletResponse response, Integer id) {
        if (id != null) {
            operaService.deleteById(id);
            return 1;
        } else {
            WebUtil.printJson(response, new Result(false).msg("日志不存在"));
        }
        return -1;
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public Integer batchDel(String ids) {
        try {
            // 将界面上的id数组格式的字符串解析成int类型的数组
            int[] arrayId = JsonUtil.json2Obj(ids, int[].class);
            operaService.deleteAll(arrayId);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
